var Role = new Ext.extend(Ext.Panel, {
	border : false,
	layout : 'border',
	initComponent : function() {
		this.initGrid();
		this.initMenuSelector();
		this.items = [this.grid, this.ms];
		Role.superclass.initComponent.call(this);
	},
	addOrUpdate : function(isAdd) {
		var me = this;
		if (this.win) {
			this.win.close();
		}
		var rec = this.grid.getRecord();
		if (!isAdd) {
			if (!rec) {
				TopMy.alert('请选择要修改的记录！');
				return;
			}
		}
		this.form = new TopExt.FormPanel({
					border : false,
					autoScroll : true,
					padding : 5,
					defaults : {
						xtype : 'textfield',
						anchor : '100%'
					},
					labelWidth : 70,
					labelAlign : 'right',
					items : [{
								xtype : 'hidden',
								name : 'roleid'
							}, {
								name : 'rolename',
								fieldLabel : '角色名称',
								allowBlank : false
							}, {
								xtype : 'textarea',
								name : 'remark',
								fieldLabel : '备&emsp;&emsp;注'
							}],
					listeners : {
						afterrender : function(t) {
							if (!isAdd) {
								t.getForm().loadRecord(rec);
							}
						}
					}
				});
		this.win = new TopExt.Window({
					title : isAdd ? '新增角色信息' : '更新角色信息',
					width : 380,
					autoHeight : true,
					modal : true,
					items : [this.form],
					buttons : [{
								text : '保存',
								iconCls : 'icon-save',
								handler : function() {
									var url = My
											.url('/system/permis/role/add.do');
									if (!isAdd) {
										url = My
												.url('/system/permis/role/update.do');
									}
									TopMy.submit(me.form, {
												url : url,
												successCallback : function() {
													me.win.close();
													me.grid.getStore().reload();
												}
											});
								}
							}, {
								text : '取消',
								iconCls : 'icon-cancel',
								handler : function() {
									me.win.close();
								}
							}]
				});
		this.win.show();
		return this.form;
	},
	initMenuSelector : function() {
		var me = this;
		this.ms = new Ext.tree.TreePanel({
			autoScroll : true,
			title : '菜单配置',
			region : 'east',
			width : 350,
			split : true,
			animate : false,
			bodyStyle : 'border-bottom:0;border-right:0;',
			headerStyle : 'border-top:0;border-right:0;',
			root : new Ext.tree.AsyncTreeNode({
						id : '0',
						text : '全选',
						expanded : true,
						checked : false,
						iconCls : '" style="background-image: url('
								+ My
										.url('/resources/images/menu/checkmark.png')
								+ ');'
					}),
			loader : new Ext.tree.TreeLoader({
						nodeParameter : 'parentid',
						baseParams : {
							roleid : '0'
						},
						dataUrl : My
								.url('/system/permis/menu/getMenuListByRole.do'),
						createNode : function(attr) {
							attr.id = attr.id || attr.menuid;
							attr.text = attr.text || attr.menuname;
							if (attr.ico) {
								attr.iconCls = '" style="background-image: url('
										+ My.url('/resources/images/menu/'
												+ attr.ico) + ');';
							}
							return Ext.tree.TreeLoader.prototype.createNode
									.call(this, attr);
						}
					}),
			reload : function() {
				this.getLoader().load(this.getRootNode(), function(root) {
							root.expand();
						});
			},
			checkChildren : function(node, checked) {
				if (node.hasChildNodes()) {
					node.eachChild(function(child) {
								child.attributes.checked = checked;
								child.getUI().checkbox.checked = checked;
								me.ms.checkChildren(child, checked);
							});
				}
			},
			checkParent : function(node) {
				var parent = node.parentNode;
				if (parent && parent.id != '0') {
					var checked = false;
					parent.eachChild(function(child) {
								if (child.attributes.checked) {
									checked = true;
								}
							});
					parent.attributes.checked = checked;
					parent.getUI().checkbox.checked = checked;
					me.ms.checkParent(parent);
				}
			},
			listeners : {
				checkchange : function(node, checked) {
					this.checkParent(node);
					this.checkChildren(node, checked);
					var roleid = this.getLoader().baseParams.roleid;
					var checkeds = this.getChecked();
					var menuids = [];
					Ext.each(checkeds, function(child) {
								if (child.id != '0') {
									menuids.push(child.id);
								}
							});
					My.request({
								loadMask : false,
								url : My.url('/system/permis/role/setMenus.do'),
								params : {
									menuids : menuids,
									roleid : roleid
								},
								success : function() {
								}
							});
				}
			}
		});
	},
	initGrid : function() {
		var me = this;
		var store = new Ext.data.JsonStore({
					fields : ['roleid', 'rolename', 'remark', 'disabled'],
					url : My.url('/system/permis/role/getRoleList.do'),
					root : 'data',
					autoLoad : true
				});
		this.grid = new Ext.grid.GridPanel({
			region : 'center',
			loadMask : true,
			getRecord : function() {
				return me.grid.getSelectionModel().getSelected();
			},
			store : store,
			sm : new Ext.grid.RowSelectionModel({
						listeners : {
							rowselect : function(t, i, r) {
								var sdbar = me.grid.getTopToolbar()
										.getComponent('set_disabled');
								var disabled = r.data.disabled;
								if (disabled == '1') {
									sdbar.setText('启用');
									sdbar.setIconClass('icon-enabled');
								} else {
									sdbar.setText('禁用');
									sdbar.setIconClass('icon-disabled');
								}
								// 加载菜单
								var rec = me.grid.getStore().getAt(i);
								me.ms.getLoader().baseParams.roleid = rec.data.roleid;
								me.ms.reload();
							}
						}
					}),
			viewConfig : {
				forceFit : true
			},
			cm : new Ext.grid.ColumnModel({
						defaults : {
							align : 'center'
						},
						columns : [{
									dataIndex : 'rolename',
									header : '角色名称'
								}, {
									dataIndex : 'remark',
									header : '备注'
								}, {
									dataIndex : 'disabled',
									header : '是否禁用',
									renderer : function(v, m) {
										if (v == '1') {
											m.attr = 'style="color:red;"';
											return '是';
										}
										m.attr = 'style="color:green;"';
										return '否';
									}
								}]
					}),
			bodyStyle : 'border-left:0;border-bottom:0;',
			tbar : new Ext.Toolbar({
						style : 'border-left:0;border-top:0;border-bottom:0;',
						items : [{
									text : '新增',
									iconCls : 'icon-add',
									handler : function() {
										me.addOrUpdate(true);
									}
								}, {
									text : '修改',
									iconCls : 'icon-update',
									handler : function() {
										me.addOrUpdate();
									}
								}, {
									text : '禁用',
									itemId : 'set_disabled',
									iconCls : 'icon-disabled',
									handler : function() {
										if (this.iconCls == 'icon-disabled') {
											me.setDisabled(true, this);
										} else {
											me.setDisabled(false, this);
										}
									}
								}, {
									text : '删除',
									iconCls : 'icon-delete',
									handler : function() {
										me.dele();
									}
								}, '-', {
									xtype : 'searchfield',
									name : 'rolename',
									store : store,
									autoSearch : true
								}]
					})
		});
	},
	setDisabled : function(disabled) {
		var me = this;
		var rec = me.grid.getRecord();
		if (disabled) {
			if (!rec) {
				My.alert('请选择要禁用的角色信息！');
				return;
			}
			My.confirm('确定要禁用该角色吗？', function(btn) {
						if (btn == 'no') {
							return;
						}
						My.request({
									url : My
											.url('/system/permis/role/setDisabled.do'),
									params : {
										roleid : rec.data.roleid,
										disabled : '1'
									},
									successCallback : function() {
										me.grid.getStore().reload();
									}
								});
					});
		} else {
			if (!rec) {
				My.alert('请选择要启用的角色信息！');
				return;
			}
			My.request({
						url : My.url('/system/permis/role/setDisabled.do'),
						params : {
							roleid : rec.data.roleid,
							disabled : '0'
						},
						successCallback : function() {
							me.grid.getStore().reload();
						}
					});
		}
	},
	dele : function() {
		var me = this;
		var rec = me.grid.getRecord();
		if (!rec) {
			My.alert('请选择要删除的角色信息 ！');
			return;
		}
		My.confirm('确定要删除该角色吗？', function(btn) {
					if (btn == 'no') {
						return;
					}
					My.request({
								url : My.url('/user/delete.do'),
								params : {
									roleid : rec.data.roleid
								},
								successCallback : function() {
									me.grid.getStore().reload();
								}
							});
				});
	}
});

Ext.onReady(function() {
			new Ext.Viewport({
						layout : 'fit',
						items : [new Role()]
					});
		});